dojo.provide("ESI.dijit.AddDataButton");

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");
dojo.require("dijit.Dialog");
			dojo.require("dijit.layout.BorderContainer");
			dojo.require("dijit.layout.ContentPane");
			dojo.require("dijit.form.ComboBox");
			dojo.require("dijit.form.Button");
			dojo.require("dojox.grid.Grid");
			dojo.require("dojox.data.jsonPathStore");
			dojo.require("dojox.jsonPath.query");
			dojo.require("dojo.data.ItemFileReadStore");
			dojo.require("dojo.io.script");
			dojo.require("dijit.Toolbar");
			dojo.declare(
'ESI.dijit.AddDataButton',
[dijit._Widget, dijit._Templated],
{
    templatePath: dojo.moduleUrl("ESI.dijit", "templates/AddDataButton.html"),
    templateCSSPath: dojo.moduleUrl("ESI.dijit", "css/AddDataButton.css"),
    widgetsInTemplate: true,
    servers: [],
    images: {
        goUp: "arrowup.png"
    },
    postCreate: function() {
        this.inherited("postCreate", arguments);
        this.setUpComboBox();
        this.refreshGridForFolder();
    },
    setUpComboBox: function() {
        var that = this;
        var data = { identifier: "name", items: [] };
        var cnt = 0;
        dojo.forEach(this.servers, function(serv) {
            data.items.push({ name: serv });
            if (cnt === 0)
                that.myComboBox.setValue(serv);
            cnt++;

        });
        var st = new dojo.data.ItemFileReadStore({ data: data });
        this.myComboBox.store = st;
        dojo.connect(this.myComboBox, "onChange", dojo.hitch(this, this.refreshGridForFolder));
        dojo.connect(this.grid, "onRowDblClick", dojo.hitch(this, this.selectService));
        dojo.connect(this.goUpButton, "onClick", dojo.hitch(this, this.goUp));
    },
    closeDialog: function() {
        this.dialog.reset();
    },
    selectService: function(evt) {
        var _row = this.grid.model.getRow(evt.rowIndex);
        var _server = this.myComboBox.getValue();

        if (_row.type === "Folder") {
            this.myComboBox.setValue(_server + "/" + _row.name);
            this.goUpButton.setDisabled(false);

        } else {
            console.log("publishing " + _server + "/" + _row.name);
            dojo.publish("/esi/add/mapservice", [_server, _row.name]);
            this.closeDialog();
        }
    },
    refreshGridForFolder: function() {
        var _server = this.myComboBox.getValue();
        var _folder = "";
        console.log(_server);
        console.log(_folder);
        dojo.io.script.get({
            url: _server + ((_folder === undefined) ? "" : ("/" + _folder)),
            content: {
                f: "json"
            },
            load: function(data) {
                var newData = [];
                dojo.forEach(data.folders, function(folder) {
                    var _item = {};
                    _item.name = folder;
                    _item.type = "Folder";
                    _item.icon = "<div style='width:20px;height:20px' class='foldericon'>";
                    newData.push(_item);
                });
                dojo.forEach(data.services, function(service) {
                    if (service.type !== "MapServer")
                        return;
                    var _item = {};

                    _item.name = service.name.substring(service.name.lastIndexOf("/") + 1);
                    _item.type = service.type;
                    _item.icon = "<div style='width:20px;height:20px' class='mapserviceicon'>";
                    newData.push(_item);
                });

                dStore = new dojox.data.jsonPathStore({ data: newData });
                dModel = new dojox.grid.data.DojoData(null, dStore, {});
                grid.setModel(dModel);
                grid.refresh();

            },
            callbackParamName: 'callback',
            timeout: 10000,
            error: dojo.hitch(this, function(resp) {
                this.infoArea.setContent("Error!  Services not found");
            })
        });

    },
    isValueBase: function(val) {
        var _isBaseValue = false;
        dojo.forEach(this.servers, function(serv) {
            if (serv === val)
                _isBaseValue = true;
        });
        return _isBaseValue;
    },
    goUp: function() {

        var _server = this.myComboBox.getValue();
        
        if (this.isValueBase(_server))
            return;

        var _newVal = _server.substring(0, _server.lastIndexOf("/"));
        if (this.isValueBase(_newVal))
            this.goUpButton.setDisabled(true);
        else    
            this.goUpButton.setDisabled(false);
        this.myComboBox.setValue(_newVal);



    }
});
